


//
//  XYImageTableViewController.m
//  十年少
//
//  Created by 尧的mac on 15/11/28.
//  Copyright © 2015年 xxy.icom. All rights reserved.
//

#import "XYImageTableViewController.h"
#import "AFNetworking.h"
#import "MJExtension.h"
#import "XYMainModel.h"
#import "MBProgressHUD+MJ.h"
#import "XYImageTableViewCell.h"
#import "XYImageFrom.h"
#import "XYMainController.h"
#import "CollectViewController.h"

@interface XYImageTableViewController ()
@property (nonatomic ,strong) NSMutableArray *array;
@property(nonatomic,assign)NSInteger index;



@end

@implementation XYImageTableViewController
-(NSMutableArray *)array{
    if (_array == nil) {
        _array = [NSMutableArray array ];
    }
    return _array;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建标题
    self.title = @"十年少";
    //刷新数据
    [self getRefreshdata];

    
    self.view.userInteractionEnabled = YES;
    UISwipeGestureRecognizer * swipRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
    
    [self.view addGestureRecognizer:swipRight];
}
-(void)swipAction:(UISwipeGestureRecognizer*)sender{
    
    if (sender.direction ==UISwipeGestureRecognizerDirectionRight) {
        XYMainController * imageContr = [[XYMainController alloc]init];
        [self.navigationController pushViewController:imageContr animated:YES];
    }
    
}

/** 刷新数据*/
-(void)getRefreshdata{
    UIRefreshControl * refresh = [[UIRefreshControl alloc]init];
    [refresh addTarget:self action:@selector(refreshAction:) forControlEvents:UIControlEventValueChanged];
    [refresh beginRefreshing];//自动刷新
    [self refreshAction:refresh];
    [self.view addSubview:refresh];
}
-(void)refreshAction:(UIRefreshControl *)sender{
    
    NSLog(@"aaaaaa");
    self.index++;
    NSString * index = [NSString stringWithFormat:@"%ld",self.index];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer  = [AFHTTPResponseSerializer serializer];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //请求头
    [manager.requestSerializer setValue:@"e2c382ce8849e756b2847a79e2cfb8a0" forHTTPHeaderField:@"apikey"];
    
    if (self.array.count) {
        params[@"page"] = index;
    }else{
        params[@"page"] = @"1";
    }
    
    
    [manager GET:@"http://apis.baidu.com/showapi_open_bus/showapi_joke/joke_pic" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSMutableDictionary *showapi_res_bodyDict  = dict[@"showapi_res_body"];
        NSMutableArray *listArray = showapi_res_bodyDict[@"contentlist"];
        NSMutableArray *mutarray = [NSMutableArray array ];
        
        for (NSDictionary * dict in listArray) {
            //数据转模型
            XYMainModel * mainModel = [XYMainModel objectWithKeyValues:dict];
            XYImageFrom * mainFrom = [[XYImageFrom alloc]init];
            mainFrom.mainModel = mainModel;
            [mutarray addObject:mainFrom];
        }
        
        //mutabArray是新数据
        //self.array是旧数据
        
        NSMutableArray * tempArray = [NSMutableArray array];
        [tempArray addObjectsFromArray:mutarray];
        [tempArray addObjectsFromArray:self.array];
        
        self.array = tempArray;
        
        //刷新数据
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [MBProgressHUD showError:@"网络请求失败"];
    }];
    
    [sender endRefreshing];
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XYImageTableViewCell * cell= [XYImageTableViewCell mainCell:tableView];
    //获取模型
    cell.mainModel =self.array[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XYImageFrom * mainModel =   self.array[indexPath.row];
    NSLog(@"%f",mainModel.cellHeight);
    return mainModel.cellHeight;
    
}


@end